package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageData;
import com.woniu.entity.dto.QueryUserTrainingDTO;
import com.woniu.entity.vo.UserTrainingVO;
import com.woniu.mapper.TrainingMapper;
import com.woniu.mapper.TrainingUseMapper;
import com.woniu.mapper.UserTrainingMapper;
import com.woniu.pojo.Training;
import com.woniu.pojo.TrainingUse;
import com.woniu.pojo.UserTraining;
import com.woniu.service.IUserTrainingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaosong
 * @since 2023-04-01
 */
@Service
public class UserTrainingServiceImpl extends ServiceImpl<UserTrainingMapper, UserTraining> implements IUserTrainingService {

    @Autowired(required = false)
    private UserTrainingMapper userTrainingMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired(required = false)
    private TrainingMapper trainingMapper;

    @Autowired(required = false)
    private TrainingUseMapper trainingUseMapper;

    @Override
    public PageData select(Integer currentPage, Integer pageSize, QueryUserTrainingDTO userTrainingDTO) {

        IPage<UserTrainingVO> page = new Page<>(currentPage, pageSize);
        IPage<UserTrainingVO> select = userTrainingMapper.select(page, userTrainingDTO);
        PageData pageData = new PageData(select.getTotal(), select.getRecords());
        return pageData;
    }

    @Override
    public boolean add(UserTraining userTraining) {
        return false;
    }

    @Override
    public boolean updateUserTraining(UserTraining userTraining) {
        return false;
    }

//    看完解析修改分数
    @Override
    public Boolean updateScore(Integer userId, Integer trainingId) {
        //        点完解析，将对应解析状态改为已查看过解析。

        QueryWrapper<UserTraining> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("training_id",trainingId);
        UserTraining  userTraining = userTrainingMapper.selectOne(wrapper);
        if (userTraining == null){
            UserTraining userTraining1 = new UserTraining();
            userTraining1.setUserId(userId);
            userTraining1.setTrainingId(trainingId);
            userTraining. setProcessStatus((byte) 1);
            userTrainingMapper.insert(userTraining1);
            return true;
        }
        userTraining. setProcessStatus((byte) 1);
        Integer num = userTrainingMapper.update(userTraining, wrapper);
        if (num>0){
            return true;
        }

        return false;
    }

    @Override
    public Boolean updateFlag(String flag, Integer userId, Integer trainingId) {
        //        获取redis数据库中的flag,查看该用户是否输入了正确的flag
//            因为我们不知道网安传回来的flag,是什么，需要网安将flag传入redis,我们来判断用户是否完成通关。
               redisTemplate.opsForValue().set("flag","qqqq");
              String flag1 = (String) redisTemplate.opsForValue().get("flag");
//              查询出该靶场的难度，来确定得分
               Training training = trainingMapper.selectById(trainingId);
               Byte difficulty = training.getDifficulty();
               Integer score = 0;
               if (difficulty ==0){
                   score = 3;
               }
              if (difficulty ==1){
                   score = 6;
               }
               if (difficulty ==2){
                   score = 9;
               }
//               判断用户传入的flag是否正确，来判断用户是否通关
               Integer num = 0;
               if (flag.equals(flag1)){
                   QueryWrapper<UserTraining> wrapper = new QueryWrapper<>();
                   wrapper.eq("user_id",userId);
                   wrapper.eq("training_id",trainingId);
                   UserTraining  userTraining = userTrainingMapper.selectOne(wrapper);
                   userTraining.setScore(score);
                    num = userTrainingMapper.update(userTraining, wrapper);

               }
//               判断是否成功
               if (num>0){
                   return true;
               }
                   return false;
    }

//  创建靶场信息
    @Override
    public Boolean createTrainingInfo(Integer userId, Integer trainingId) {
//        创建用户靶场信息
        QueryWrapper<UserTraining> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("training_id",trainingId);
        UserTraining  userTraining = userTrainingMapper.selectOne(wrapper);
        if (userTraining ==null){
            UserTraining userTraining1 = new UserTraining();
            userTraining1.setUserId(userId);
            userTraining1.setTrainingId(trainingId);
            Integer i = userTrainingMapper.insert(userTraining1);
        }
//        创建靶场用户信息

         TrainingUse trainingUse = new TrainingUse();
         trainingUse.setUserId(userId);
         trainingUse.setTrainingId(trainingId);
         Integer i = trainingUseMapper.insert(trainingUse);
         if (i >0){
             return true;
         }

        return false;
    }
}
